In [1]:
%run tabipy.py

In [2]:
t = Table((1,2,3),
          (4,5,6),
          (7,8,9))
t


Out[2]:
123
456
789

In [3]:
cell_1_1 = t.rows[0].cells[0]
cell_1_1.col_span = 2

In [4]:
t


Out[4]:
13
456
789

In [5]:
t1_html = t._repr_html_()
print(t1_html)
t1_html


<table>
<tr><td colspan="2"  >1</td><td  >3</td></tr>
<tr><td  >4</td><td  >5</td><td  >6</td></tr>
<tr><td  >7</td><td  >8</td><td  >9</td></tr>
</table>
Out[5]:
'<table>\n<tr><td colspan="2"  >1</td><td  >3</td></tr>\n<tr><td  >4</td><td  >5</td><td  >6</td></tr>\n<tr><td  >7</td><td  >8</td><td  >9</td></tr>\n</table>'

In [6]:
t1_latex = t._repr_latex_()
print(t1_latex)


\begin{tabular}{*{3}{l}}
\multicolumn{2}{l}{1} & 3\\

4 & 5 & 6\\

7 & 8 & 9\\
\end{tabular}

In [7]:
t1_latex


Out[7]:
'\\begin{tabular}{*{3}{l}}\n\\multicolumn{2}{l}{1} & 3\\\\\n\n4 & 5 & 6\\\\\n\n7 & 8 & 9\\\\\n\\end{tabular}'

In [8]:
t = Table((TableCell(1,col_span=2),2,3),
          (4,5,6,7),
          (8,9,10,11))
t


Out[8]:
123
4567
891011

In [9]:
print(t._repr_latex_())


\begin{tabular}{*{4}{l}}
\multicolumn{2}{l}{1} & 2 & 3\\

4 & 5 & 6 & 7\\

8 & 9 & 10 & 11\\
\end{tabular}

In [10]:
r1 = t.rows[0]
c1 = r1.cells[0]
c1.col_span=1

In [11]:
t


Out[11]:
123
4567
891011

In [12]:
print(t._repr_html_())


<table>
<tr><td  >1</td><td  ></td><td  >2</td><td  >3</td></tr>
<tr><td  >4</td><td  >5</td><td  >6</td><td  >7</td></tr>
<tr><td  >8</td><td  >9</td><td  >10</td><td  >11</td></tr>
</table>

Automated Tests


In [13]:
import re

def col_span_table():
    t = Table((1,2,3),
      (4,5,6),
      (7,8,9))
    cell_1_1 = t.rows[0].cells[0]
    cell_1_1.col_span = 2
    return t

def test_col_span_html():
    "This test col_span works in html"
    t = col_span_table()
    t1_html = t._repr_html_()
    row_split = re.compile('<\s*tr\s*>')
    lines = row_split.split(t1_html)
    assert len(lines)==4
    col_split = re.compile('>[\s\d\s]*<')
    parts = col_split.split(lines[1])
    cl_check = re.compile('colspan\s*=\s*"\s*2\s*"')
    assert len(cl_check.findall(parts[0]))>0
    #print("pass")

def test_col_span_latex():
    "This test col_span works in latex"
    t = col_span_table()    
    t1_latex = t._repr_latex_()
    row_split = re.compile(r'\\\\')
    lines = row_split.split(t1_latex)
    assert len(lines)==4
    col_split = re.compile('&')
    parts = col_split.split(lines[0])
    cl_check = re.compile('\w*\\multicolumn\s*\{\s*2\s*}')
    assert len(cl_check.findall(parts[0]))>0
    #print("pass")
    
test_col_span_html()
test_col_span_latex()